using UnityEngine;

public class HP : MonoBehaviour 
{
	public GameObject hpGameObject;
    public GameObject lifeGameObject;
	private int hp = 4;
    private int life = 5;
    private Texture2D hpTexture;
    private Texture2D lifeTexture;
    public Texture2D[] hpImages = new Texture2D[5];
    public Texture2D[] lifeImages = new Texture2D[5];

    public Vector3 startPosition;
    public string loseFase;
	
	public GameObject checkpointGameObject;
	private Checkpoint checkpoint;

    void Start()
    {
        startPosition = transform.position;
		checkpoint = checkpointGameObject.GetComponent<Checkpoint>();
    }

	void Update () 
	{
		if(life <= 0)
		{
            Application.LoadLevel(loseFase);
		}
		
        hpTexture = hpImages[hp];
        hpGameObject.renderer.material.mainTexture = hpTexture;

        lifeTexture = lifeImages[life - 1];
        lifeGameObject.renderer.material.mainTexture = lifeTexture;
	}
	
	public void Damage(int damage)
	{
        if(hp > 0)
        {
            hp -= damage; 
        }

        if (hp <= 0)
        {
            life--;
            hp = 4;

            transform.position = checkpoint.getPosition();
        }
	}
	
	public void setLife(int life)
	{
		this.life = life;
	}
	
	public int getLife()
	{
		return life;
	}
	
	public void increaseLife(int num)
	{
		this.life += num;
	}
}
